﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LCA.Community.DAL.Model;
using LCA.Community.DAL.Interfaces;

namespace LCA.Community.DAL
{
    /// <summary>
    /// The sepository for groups membership data manipulation.
    /// </summary>
    public class GroupMembershipsRepository : BaseRepository<GroupMembership>, IGroupMembershipsRepository
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="GroupMembershipsRepository"/> class.
        /// </summary>
        /// <param name="dataServiceContext">The data service context.</param>
        public GroupMembershipsRepository(IDataServiceContext<GroupMembership> dataServiceContext)
            :base(dataServiceContext)
        {
    
        }

        /// <summary>
        /// Adds the a group membership given the user and group id.
        /// </summary>
        /// <param name="groupID">The group ID.</param>
        /// <param name="userID">The user ID.</param>
        public void Add(string groupID, string userID)
        {
            Add(new GroupMembership(userID, groupID));
            Context.SaveChanges();
        }

        /// <summary>
        /// Gets the group membership of a specific user.
        /// </summary>
        /// <param name="groupID">The group ID.</param>
        /// <param name="userID">The user ID.</param>
        /// <returns>The group membership.</returns>
        public GroupMembership Get(string groupID, string userID)
        {
            return (from groupMembership in Entities
                    where groupMembership.Group == groupID && groupMembership.Member == userID
                    select groupMembership).FirstOrDefault();
        }

        /// <summary>
        /// Remove a membership given the user and the group id.
        /// </summary>
        /// <param name="groupID">The group ID.</param>
        /// <param name="userID">The user ID.</param>
        public void Delete(string groupID, string userID)
        {
            var membership = Get(groupID, userID);
            Delete(membership);
        }

        /// <summary>
        /// Gets the group membership by group.
        /// </summary>
        /// <param name="groupID">The group ID.</param>
        /// <returns>The group memberships of the group.</returns>
        public List<GroupMembership> GetGroupMembershipByGroup(string groupID)
        {
            return (from groupMembership in Entities
                    where groupMembership.Group == groupID
                    select groupMembership).ToList();
        }

        /// <summary>
        /// Gets the group membership by user.
        /// </summary>
        /// <param name="userID">The user ID.</param>
        /// <returns>The groups membership of the user.</returns>
        public List<GroupMembership> GetGroupMembershipByUser(string userID)
        {
            return (from groupMembership in Entities
                    where groupMembership.Member == userID
                    select groupMembership).ToList();
        }

        /// <summary>
        /// Deletes all memberships of the specified user.
        /// </summary>
        /// <param name="userID">The user ID.</param>
        public void DeleteAllUserMemberships(string userID)
        {
            var memberships = Entities.Where(m => m.Member == userID).ToList();
            memberships.ForEach(m => Delete(m));
        }

        /// <summary>
        /// Deletes all memberships of the specified group.
        /// </summary>
        /// <param name="userID">The user ID.</param>
        public void DeleteAllGroupMemberships(string groupID)
        {
            var memberships = Entities.Where(m => m.Group == groupID).ToList();
            memberships.ForEach(m => Delete(m));
        }
    }
}
